package it.hive.io.properties;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

public class IOManager {

	/* ##################################### METODI PUBBLICI #################################### */
	public static Properties leggiPropertiesFile(String path, String fileName) {
		String pathFormattato = formattaPath(path);
		String propertiesFile = formattaNomeFile(fileName);

		Properties dbProp = new Properties();
		try {
			dbProp.load(new FileReader(pathFormattato.concat(propertiesFile)));
		} catch (FileNotFoundException e) {
			return null;
		} catch (IOException e) {
			return null;
		}
		return dbProp;
	}

	public static boolean scriviPropertiesFile(String path, String fileName, Properties prop) {
		String pathCompleto = formattaPath(path).concat(formattaNomeFile(fileName));
		try {
			prop.store(new FileWriter(pathCompleto), "");
		} catch (IOException e) {
			return false;
		}
		return true;
	}

	@SuppressWarnings({ "unchecked", "resource" })
	public static <T extends Serializable> T leggiOggetto(String path, String fileName) {
		String pathFormattato = formattaPath(path);
		String serializableData = formattaNomeFile(fileName);

		try {
			FileInputStream fin = new FileInputStream(pathFormattato.concat(serializableData));
			return (T) new ObjectInputStream(fin).readObject();
		} catch (ClassNotFoundException | IOException e) {
			return null;
		}
	}

	@SuppressWarnings("resource")
	public static <T extends Serializable> boolean scriviOggetto(String path, String fileName, T object) {
		try {
			FileOutputStream fos = new FileOutputStream(formattaPath(path).concat(formattaNomeFile(fileName)));
			new ObjectOutputStream(fos).writeObject(object);
		} catch (IOException e) {
			return false;
		}
		return true;
	}

	/* ##################################### METODI PRIVATI ##################################### */
	private static String formattaNomeFile(String fileName) {
		// "config//DataBaseConfiguration"
		String name = fileName.trim();
		String result = null;
		if (StringUtils.isNotBlank(name)) {
			String[] splittedName = name.split("/");
			if (splittedName.length > 1) {
				result = "";
				for (String s : splittedName) {
					result = result.concat(s);
				}
			} else {
				result = name;
			}
		}
		return result;
	}

	private static String formattaPath(String path) {
		String result = null;
		if (StringUtils.isNotBlank(path)) {
			result = "";
			String[] splittedName = path.split("/");
			if (splittedName.length > 1) {
				for (String s : splittedName) {
					result = result.concat(s).concat("\\");
				}
			} else {
				result = result.concat(path.replace("/", "\\")).concat("\\");
			}
		}
		return result;
	}
}
